package com.vehicle.controller;

import com.vehicle.bean.DataGridView;
import com.vehicle.bean.ResultDto;
import com.vehicle.pojo.Car;
import com.vehicle.service.CarService;
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/car")
public class CarController extends BaseController{
    @Autowired
    private CarService carService;
    private Logger logger = Logger.getLogger(this.getClass());

    public CarController() {
    }

    @RequestMapping({"/cars"})
    @ResponseBody
    public DataGridView list(Car car) {
        return this.carService.findCarByPage(car);
    }

    //展示所有的汽车状态
    @RequestMapping("selectCarStatus")
    @ResponseBody
    public DataGridView selectCarStatus(Car car){
        return carService.findCarStatus(car);
    }


    @RequestMapping({"/allcar"})
    @ResponseBody
    public DataGridView allCar() {
        DataGridView dataGridView=new DataGridView();
        dataGridView.setData(carService.findAllCar());
        return dataGridView;
    }
    //根据id 返回Car
    @ResponseBody
    @RequestMapping(value = "/car")
    public ResultDto findCarByCarid(Integer carid){
        ResultDto resultDto = new ResultDto();
        resultDto.setData(carService.findCarByCarid(carid));
        return resultDto;
    }
    //增加车辆
    @ResponseBody
    @RequestMapping("/addCar")
    public ResultDto addCar(Car car, @RequestParam("file") MultipartFile file, HttpServletRequest request){
        //获取根路径
        String rootPath= request.getServletContext().getRealPath("/static/img/");
        //获取图片路径
        String imgPath=file.getOriginalFilename();
        //p拼接上传路径
        String path=rootPath+imgPath;
        car.setImg_url("/static/img/"+imgPath);
        ResultDto resultDto=new ResultDto();
        try {
            file.transferTo(new File(path));
            carService.addCar(car);
            logger.info(car);
            resultDto.setCode(200);
            resultDto.setMsg("添加成功");
            resultDto.setData(car);
        } catch (Exception e) {
            resultDto.setCode(-200);
            resultDto.setMsg("添加失败");
            e.printStackTrace();
        }
        return resultDto;
    }

    //修改车辆信息
    @ResponseBody
    @RequestMapping("/editCar")
    public ResultDto editCar(Car car,@RequestParam("file") MultipartFile file,HttpServletRequest request){
        logger.info(car);
        String realPath = request.getServletContext().getRealPath("/static/img/");
        String imgPath=file.getOriginalFilename();
        String path=realPath+imgPath;

        ResultDto resultDto=new ResultDto();
        long size = file.getSize();
        if (size>0){
            try {

                Car carByCarid = carService.findCarByCarid(car.getCarid());
                String headimg = carByCarid.getImg_url();
                if (headimg!=null){
                    String aA=request.getServletContext().getRealPath("/")+headimg;
                    logger.info(aA);
                    File headimgPath=new File(aA);
                    headimgPath.delete();
                }
                car.setImg_url("/static/img/"+imgPath);
                file.transferTo(new File(path));
                carService.editCar(car);
                resultDto.setMsg("修改成功");
                resultDto.setCode(200);
                resultDto.setData(car);
            } catch (IOException e) {
                resultDto.setMsg("修改失败");
                resultDto.setCode(-200);
                e.printStackTrace();
            }
        }else {
            try {
                carService.editCar(car);
                resultDto.setMsg("修改成功");
                resultDto.setCode(200);
                resultDto.setData(car);
            } catch (Exception e) {
                resultDto.setMsg("修改失败");
                resultDto.setCode(-200);
                e.printStackTrace();
            }
        }
        return  resultDto;
    }

    //修改车辆状态单个删除
    @ResponseBody
    @RequestMapping("/delete")
    public ResultDto deleteCar(Integer carid){
        ResultDto resultDto=new ResultDto();
        try {
            carService.updateStatus(carid);
            resultDto.setMsg("删除成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultDto;
    }
    @ResponseBody
    @RequestMapping("/deleteBatch")
    public ResultDto deleteBatchCar(@RequestParam("carids[]") Integer[] carids){
        ResultDto resultDto=new ResultDto();
        try {
            carService.updateBatchStatus(carids);
            resultDto.setMsg("删除成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultDto;
    }
}
